/**   
 * @Title: ROJSONUtil.java 
 * @Package com.shangmei.api.util
 * @Description: TODO 
 */

package com.amani.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.json.SerializationParams;

/**
 * @ClassName: ROJSONUtil
 * @Description: TODO
 * 
 */

public class ROJSONUtil {
	private static final Logger LOG = Logger.getLogger(ROJSONUtil.class);

	public static void writeJSONToResponse(
			SerializationParams serializationParams) throws IOException {
		StringBuilder stringBuilder = new StringBuilder();
		if (StringUtils.isNotBlank(serializationParams.getSerializedJSON()))
			stringBuilder.append(serializationParams.getSerializedJSON());

		if (StringUtils.isNotBlank(serializationParams.getWrapPrefix()))
			stringBuilder.insert(0, serializationParams.getWrapPrefix());
		else if (serializationParams.isWrapWithComments()) {
			stringBuilder.insert(0, "/* ");
			stringBuilder.append(" */");
		} else if (serializationParams.isPrefix())
			stringBuilder.insert(0, "{}&& ");

		if (StringUtils.isNotBlank(serializationParams.getWrapSuffix()))
			stringBuilder.append(serializationParams.getWrapSuffix());

		String json = stringBuilder.toString();

		if (LOG.isDebugEnabled()) {
			LOG.debug("[JSON]" + json);
		}

		HttpServletResponse response = serializationParams.getResponse();

		// status or error code
		if (serializationParams.getStatusCode() > 0)
			response.setStatus(serializationParams.getStatusCode());
		else if (serializationParams.getErrorCode() > 0)
			response.sendError(serializationParams.getErrorCode());

		// content type
		if (serializationParams.isSmd())
			response.setContentType("application/json-rpc;charset="
					+ serializationParams.getEncoding());
		else
			response.setContentType(serializationParams.getContentType()
					+ ";charset=" + serializationParams.getEncoding());

		if (serializationParams.isNoCache()) {
			response.setHeader("Cache-Control", "no-cache");
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "No-cache");
		}

		if (serializationParams.isGzip()) {
			response.addHeader("Content-Encoding", "gzip");
			GZIPOutputStream out = null;
			InputStream in = null;
			try {
				out = new GZIPOutputStream(response.getOutputStream());
				/**
				 * 修正编码问题
				 */
				in = new ByteArrayInputStream(json.getBytes(serializationParams
						.getEncoding()));
				byte[] buf = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
			} finally {
				if (in != null)
					in.close();
				if (out != null) {
					out.finish();
					out.close();
				}
			}

		} else {
			response.setContentLength(json.getBytes(serializationParams
					.getEncoding()).length);
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}
}
